%% Arrays and Indexing

%create an 10 x 1 sized array with all elements set to zero
a=zeros(10,1)

%create an 1 x 10 sized array with all elements set to zero
b=zeros(1,10)

%create an 5 x 5 sized array with all elements set to zero
c=zeros(5,5)

%create an array large enough to handle the assigned element (not
%recommended)
d(5)=6

%make a matrix 'by hand'
X=[1,2;3,4]

%get the element down one column, over 2 columns.  
X(1,2)

%get the entire first column
X(:,1)

%get the entire first row
X(1,:)

%generate a 2x4 size matrix of random numbers. 
Y=rand(2,4)

%get the top row,  middle columns elements
Y(1,2:3)

%assign the top row,  middle columns elements the value '7'
Y(1,2:3)=7

%write bottom-middle elements to top-middle
N=Y(2,2:3)
Y(1,2:3)=N

%create a row vector
Z=(1:10)

Z=(1:.1:10)

%tip: look at function 'linspace',
%   linspace(X1, X2) generates a row vector of 100 linearly
%       equally spaced points between X1 and X2.



%% Plotting

X=(0:.1:20);    %semicolon suppresses output
Y=cos(X)
Y2=sin(X)

%create a figure and plot two vectros
figure
plot(X,Y,'r','linewidth',4)
xlabel('X (s)','fontsize',16)
ylabel('Y (m)','fontsize',16)
title('voltage from scope')
hold on
plot(X,Y2)
legend('voltage 1','voltage 2')

%see 'help plot' for more plotting options. 


%% Functions

%see AddVars.m

%call our new function:

C=AddVars(1,2)

%AddVars must be in current directory or in MATLAB's path:
path

%check which version we are using
which AddVars

help AddVars

%% Printing to Command Window

%use 'help fprintf' to see all options. 

%Example:

fprintf('Here is some text!\r') % the '\r' is the carriage return. Try it without. 
fprintf('Here is some text!\n') % the '\n' is the 'new line' character. 
%I usually use '\n'. 

%you can find all the 'escape characters' in 'help fprintf'
%These do not print out directly, but indicate some behaviour

%you can print numbers or variables in text like this:

fprintf('Result is: %d \n',4)
% '%d' is for integers.  For other data types see:
doc fprintf
%and links to formatting strings

%A few more examples

%string
fprintf('Result is: %s \n','Pineapples')

%double variable
x=555.345
fprintf('Result is: %g \n',x)

%loop
for ii=1:10
    fprintf('Result is: %d \n',ii)
end

%% Conditional Statements: if

a=2
b=1

if (a>b)    %then do something
    fprintf('conditional was true\n')
end

if (b>a)    %then do something
    fprintf('conditional was true\n')
else  %then do something else
    fprintf('conditional was false\n')
end

if (b>a)    %then do something
    fprintf('first conditional was true\n')
elseif (b==a) %then do this
    fprintf('second conditional was true\n')
else    %none of above was true, then do this:
    fprintf('no conditional was true\n')
end

%see
help if

%example

for ii=1:10
    if ii>5
        fprintf('Found this: %d\n',ii);
    end
end
















































